package com.roc.web.action.terminal;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

public class YsbTermSnStatisticsSearchCondition extends BaseSearchCondition  {
	
	
	private static final long serialVersionUID = 1L;

	private String agentId;
	private String roleType;//角色类型
	private String assefFlag;// 资产类型
	
	@Override
	protected String getOrderPartOfHQL() {
		// TODO Auto-generated method stub
		
		return " ";
	}
	@Override
	protected String getRowCountHQL() {
		
		
		StringBuffer sb=new StringBuffer();
		sb.append(" select count(*) from (  ");
		sb.append(" 	select TM.asset_flag ");
		sb.append(" 	  from (select a.asset_flag, 1 cou, t.device_cd b_cou ");
		sb.append(" 	          from (select distinct i1.asset_flag, ");
		sb.append("             	   i1.serial_num, ");
		sb.append("              	  i1.pos_brand ");
		sb.append("           		from t_pos_info i1, t_pos_agent p1  ");

		// 查询账号角色非系统管理员 查询当前账号下所有代理商
		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {

			sb.append(" 	, (SELECT TO_CHAR(T.ID) TID "  );
			sb.append(" 	         FROM T_OPERATORS T " );
			sb.append(" 	      START WITH ID = " + agentId + " " );
			sb.append(" 	    CONNECT BY PRIOR T.ID = T.SUPRI_ID " );
			sb.append(" 	      ORDER BY T.ID DESC) TT " );
			
		}
		sb.append("          where i1.serial_num = p1.serial_num and i1.pos_brand = p1.pos_brand ");
		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {
			sb.append("           	and p1.agent_id = TT.TID  " );
		}
		if (Constance.isNotEmpty(assefFlag) && !assefFlag.equals("-1")) {
			sb.append("           	and i1.asset_flag = " + assefFlag.trim() );
		}
		
		sb.append("   	        ) a ");
		sb.append("           left join tbl_term_inf t on a.serial_num = t.device_cd ");
		sb.append("                                   and a.pos_brand = t.tmnl_brand ");
		sb.append("                                   and instr(t.DEVICE_CD, 'YJ') <= 0 ");
		sb.append("                                   and t.REC_UPD_TS is not null ) TM ");
		sb.append(" 	 group by TM.asset_flag ) TS ");

		return sb.toString();
	}

	@Override
	protected String getSearchHQL() {
		StringBuffer sb=new StringBuffer();

		sb.append(" select TM.asset_flag, sum(TM.cou) a_sum, count(TM.b_cou) b_sum ");
		sb.append(" 	  from (select a.asset_flag, 1 cou, t.device_cd b_cou ");
		sb.append(" 	          from (select distinct i1.asset_flag, ");
		sb.append("             	   i1.serial_num, ");
		sb.append("              	  i1.pos_brand ");
		sb.append("           		from t_pos_info i1, t_pos_agent p1  ");

		// 查询账号角色非系统管理员 查询当前账号下所有代理商
		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {

			sb.append(" 	, (SELECT TO_CHAR(T.ID) TID "  );
			sb.append(" 	         FROM T_OPERATORS T " );
			sb.append(" 	      START WITH ID = " + agentId + " " );
			sb.append(" 	    CONNECT BY PRIOR T.ID = T.SUPRI_ID " );
			sb.append(" 	      ORDER BY T.ID DESC) TT " );
			
		}
		sb.append("          where i1.serial_num = p1.serial_num and i1.pos_brand = p1.pos_brand ");
		if ( !roleType.equals(Role.SYSTEM.getCode()) ) {
			sb.append("           	and p1.agent_id = TT.TID  " );
		}
		if (Constance.isNotEmpty(assefFlag) && !assefFlag.equals("-1")) {
			sb.append("           	and i1.asset_flag = " + assefFlag.trim() );
		}
		sb.append("   	        ) a ");
		sb.append("           left join tbl_term_inf t on a.serial_num = t.device_cd ");
		sb.append("                                   and a.pos_brand = t.tmnl_brand ");
		sb.append("                                   and instr(t.DEVICE_CD, 'YJ') <= 0 ");
		sb.append("                                   and t.REC_UPD_TS is not null ) TM ");
			       
		sb.append("  group by TM.asset_flag ");

		return sb.toString(); //y.isBind = 'N'
	}

	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();

		return filedBeanList;
	}

	public String getAgentId() {
		return agentId;
	}

	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}
	public String getAssefFlag() {
		return assefFlag;
	}
	public void setAssefFlag(String assefFlag) {
		this.assefFlag = assefFlag;
	}
}
